Golang : Convert a rune to unicode style string \u
Continuing from previous tutorial on how to get escape characters or \u unicode style string. These are some additional methods to convert rune to unicode style string.
package main
import (
"fmt"
)
func main() {
// use %x for single rune
fmt.Printf("\\u%x\n", 'お')
// fmt.Printf("\\u%x\n", 'おはよう') -- will not work
// use %+q for multiple runes
fmt.Printf("%+q\n", "おはよう")
}
output :
\u304a
"\u304a\u306f\u3088\u3046"
Another way :
package main
import (
"fmt"
)
func main() {
u := fmt.Sprintf("%U", 'お')
fmt.Println(u)
}
Output :
U+304A
or use strconv.QuoteRuneToASCII()
function.
package main
import (
"fmt"
"strconv"
)
func main() {
// translate character to rune
r := rune('お')
// extra the unicode \uxxxx value
unicode := strconv.QuoteRuneToASCII(r)
fmt.Println(string(r)+" unicode string value is : ", unicode)
}
Output :
お unicode string value is : '\u304a'
Reference :
See also : Golang : Get escape characters \u form from unicode characters
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.4k Golang : Individual and total number of words counter example
+8.8k Golang : Executing and evaluating nested loop in html template
+33.9k Golang : Call a function after some delay(time.Sleep and Tick)
+7.3k Golang : Of hash table and hash map
+6k Fontello : How to load and use fonts?
+6.3k Golang : How to get capacity of a slice or array?
+5.5k Golang : Stop goroutine without channel
+14.6k Golang : Convert(cast) int to float example
+16.6k Golang : Delete files by extension
+10.1k Golang : Edge detection with Sobel method
+6.3k Apt-get to install and uninstall Golang
+30.4k Golang : How to verify uploaded file is image or allowed file types